package com.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.DBUtils.DBUtil;

/**
 * Servlet implementation class ViewPostServlet
 */
@WebServlet("/ViewPostServlet")
public class ViewPostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewPostServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession(true);
		Object name=session.getAttribute("name");
//		System.out.println(name);
		
		String grade=request.getParameter("grade");
		String comment=request.getParameter("comment");
		String id=request.getParameter("id");
		int topicId=Integer.parseInt(id);
//		System.out.println(comment);
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date=sdf.format(new Date());
		
		try {
			Connection con=DBUtil.getconn();
			StringBuffer sql1=new StringBuffer("insert into bbsreply(reply,replyDate,FK_t,replyNickName,grade) values(' ");
			sql1.append(comment);
			sql1.append("','");
			sql1.append(date);
			sql1.append("',");
			sql1.append(topicId);
			sql1.append(",'");
			sql1.append(name);
			sql1.append("',");
			sql1.append(grade);
			sql1.append("')");
//			System.out.println(sql1.toString());
			Statement stt=con.createStatement();
			stt.executeUpdate(sql1.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
